package com.jule.teenpatti.game.network.protocol.reqs;

import com.jule.teenpatti.game.network.protocol.ClientReq;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 申请入桌(因为是自动分桌并且坐下，因此可以不再需要此接口，让客户端直接调用sitDown接口即可)
 * return：
 * -1 玩家必须自动坐下
 * -2 桌子不存在
 * -3 已加入房间，不能重复加入
 * -4 房间人数已满
 */
@Deprecated
public class JoloGame_ApplyJoinTableReq_50000 extends ClientReq {
    private final static Logger logger = LoggerFactory.getLogger(JoloGame_ApplyJoinTableReq_50000.class);

    public JoloGame_ApplyJoinTableReq_50000(int functionId) {
        super(functionId);
    }

    //private JoloGame.JoloGame_ApplyJoinTableReq req;


    @Override
    public void readPayLoadImpl(ByteBuf buf) throws Exception {
        byte[] blob = new byte[buf.readableBytes()];
        buf.readBytes(blob);
//        req = JoloGame.JoloGame_ApplyJoinTableReq.parseFrom(blob);
//        TableInfo table = TableService.getInstance().getTable(req.getTableId());
//        if(null != table){
//            this.setTable(table);
//        }else{
//            logger.error("桌子不存在,roomId->"+req.getRoomId()+", tableId->"+req.getTableId());
//        }
    }

    @Override
    public void processImpl() throws Exception {
//        logger.debug("收到消息-> " + functionId + " reqNum-> " + header.reqNum + "msg ->" + req.toString());
//        JoloGame.JoloGame_ApplyJoinTableAck.Builder builder = JoloGame.JoloGame_ApplyJoinTableAck.newBuilder();
//        String userId = req.getUserId();
//        String roomId = req.getRoomId();
//        String tableId = req.getTableId();
//        int isAutoSitDown = req.getIsAutoSitDown();
//
//        if(isAutoSitDown == 0){
//            ctx.writeAndFlush(new JoloGame_ApplyJoinTableAck_50000(builder
//                    .setUserId(userId)
//                    .setRoomId(roomId)
//                    .setTableId(tableId)
//                    .setResult(-1)
//                    .setResultMsg("玩家必须自动坐下。roomId->"+req.getRoomId()+", tableId->"+req.getTableId())
//                    .build(), header));
//            return;
//        }
//
//        TableInfo table = getTable();
//
//        if (table == null) {
//            ctx.writeAndFlush(new JoloGame_ApplyJoinTableAck_50000(builder
//                    .setUserId(userId)
//                    .setRoomId(roomId)
//                    .setTableId(tableId)
//                    .setResult(-2)
//                    .setResultMsg("桌子不存在.roomId->"+req.getRoomId()+", tableId->"+req.getTableId())
//                    .build(), header));
//            return;
//        }
//        if (table.isHasPlayer(userId)) {
//            ctx.writeAndFlush(new JoloGame_ApplyJoinTableAck_50000(builder
//                    .setUserId(userId)
//                    .setRoomId(roomId)
//                    .setTableId(tableId)
//                    .setResult(-3)
//                    .setResultMsg("已经加入房间，不能重复加入")
//                    .build(), header));
//            return;
//        }
//
//        if (table.getAllPlayers().size() >= Config.PLAYER_LIMIT) {
//            ctx.writeAndFlush(new JoloGame_ApplyJoinTableAck_50000(builder
//                    .setUserId(userId)
//                    .setRoomId(roomId)
//                    .setTableId(tableId)
//                    .setResult(-4)
//                    .setResultMsg("房间人数已满，人数限制->"+Config.PLAYER_LIMIT)
//                    .build(), header));
//            return;
//        }
//
//        //进入房间不入座
//        //需要优化
//        //用户信息：现在从数据库获取，以后可优化成从缓存获取
//        User user = DBUtil.selectByPrimaryKey(userId);
//        PlayerInfo player = new PlayerInfo(table.getTableId(), user.getId(), user.getNick_name(), user.getIco_url());
//
//        //修改相关map内容，增加新玩家入桌状态
//        table.addPlayer(player.getPlayerId(), player);
//        UserTableService.getInstance().onPlayerInTable(player, table);
//        RoomStateService.getInstance().onPlayerJoinTable(table);

        //TODO：自动坐下

        //List<JoloGame.JoloGame_TablePlay_PlayerInfo> list = table.getPlayers();
//        if (table == null) {
//            ctx.writeAndFlush(new JoloGame_ApplyJoinTableAck_50000(builder
//                    .setUserId(userId)
//                    .setRoomId(roomId)
//                    .setTableId(tableId)
//                    .setResult(0)
//                    .setResultMsg("桌子不存在.roomId->"+req.getRoomId()+", tableId->"+req.getTableId())
//                    .build(), header));
//            return;
//        }
//        if (table.isHasPlayer(userId)) {
//            ctx.writeAndFlush(new JoloGame_ApplyJoinTableAck_50000(builder
//                    .setUserId(userId)
//                    .setRoomId(roomId)
//                    .setTableId(tableId)
//                    .setResult(0)
//                    .setResultMsg("已经加入房间，不能重复加入")
//                    .build(), header));
//            return;
//        }
//
//        if (table.getTableMap().size() >= Config.PLAYER_LIMIT) {
//            ctx.writeAndFlush(new JoloGame_ApplyJoinTableAck_50000(builder
//                    .setUserId(userId)
//                    .setRoomId(roomId)
//                    .setTableId(tableId)
//                    .setResult(0)
//                    .setResultMsg("房间人数已满，人数限制->"+Config.PLAYER_LIMIT)
//                    .build(), header));
//            return;
//        }
//
//        //进入房间不入座
//        //需要优化
//        User user = DBUtil.selectByPrimaryKey(userId);
//        Player player = new Player(user.getId(), user.getNick_name(), user.getIco_url());
//        //现在从数据库获取 或者从缓存
//        table.addPlayer(player.getPlayerId(), player);
//        UserTableService.getInstance().onPlayerInTable(player, table);
//        RoomStateService.getInstance().onPlayerJoinTable(table);
//        List<JoloGame.JoloGame_TablePlay_PlayerInfo> list = table.getPlayers();getPlayers
//        ctx.writeAndFlush(new JoloGame_ApplyJoinTableAck_50000(builder
//                .setUserId(userId)
//                .setRoomId(roomId)
//                .setTableId(tableId)
//                .setResult(1)
//                .setCurrPlayStatus(table.getETableState().getValue())
//                .setCurrCountDown(15)//倒计时秒数 TODO
//                .addAllPlayerInfoList(list)
//                .build(), header));
    }
}
